package com.yssoft.controller.app.sales;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yssoft.service.sales.SaleBillService;
import com.yssoft.utils.AppUtil;

@CrossOrigin
@RequestMapping("/app/salebill")
@Controller
public class SaleBillController {
	private static Logger logger=Logger.getLogger(SaleBillController.class);
	@Resource
	private SaleBillService billService;
	
	@ResponseBody
	@RequestMapping(value = "findOrderPage",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findOrderPage(int page, int limit, @RequestParam Map<String, Object> param, HttpServletRequest request) {
		String msg = "销货订单查询列表"; 
		try{
			//param参数列表（客户或者供应商id， 
    		List<Map<String,Object>> pageList0 = billService.findOrderPage(page, limit, param, request);
    		return AppUtil.returnListJson("1", msg+"成功!", pageList0);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	 
	}
	@ResponseBody
	@RequestMapping(value = "findBillPage",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findBillPage(Integer currentPage, int pageSize, @RequestParam Map<String, Object> param, HttpServletRequest request) {
		String msg = "销货单查询列表";
		try{
			//param参数列表（客户或者供应商id，//销售单状态：见码表 0-待处理 1-已处理 salesOrderStat）
    		List<Map<String,Object>> pageList = billService.findBillPage(currentPage, pageSize, param, request);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	 
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	 
	}
	@ResponseBody
	@RequestMapping(value = "findOrderDetail",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findOrderDetail(@RequestParam Map<String, Object> param ) {
		String msg = "销货订单查看";
    	try{
    		Map<String,Object> custom = billService.findOrderDetail(param);
    		return AppUtil.returnMapJson("1", msg+"成功!", custom);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "findBillDetail",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findBillDetail(@RequestParam Map<String, Object> param , HttpServletRequest request) {
		String msg = "销货单查看";
    	try{
    		Map<String,Object> custom = billService.findBillDetail(param,request);
    		return AppUtil.returnMapJson("1", msg+"成功!", custom);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	/**
	 * 
    * @Title: 查询仓库列表
    * @Description: TODO(这里用一句话描述这个方法的作用)
    * @param @param param
    * @param @param request
    * @param @return    参数
    * @return Object    返回类型
    * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "findWarehouseList",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findWarehouseList(@RequestParam Map<String, Object> param , HttpServletRequest request) {
		String msg = "仓库列表查看";
    	try{
    		List<Map<String, Object>> warehouseList = billService.findWarehouseList(param,request);
    		return AppUtil.returnListJson("1", msg+"成功!", warehouseList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	@ResponseBody
	@RequestMapping(value = "createBills",method = {RequestMethod.POST,RequestMethod.GET})
	public Object createBills(@RequestParam Map<String, Object> param ,  HttpServletRequest request) {
		String msg = "创建销货单号";
		 
		
		try{
			  
			int orderId=billService.createBills(param,request);
			//更新订单主单状态,明细金额
			billService.updateSalesOrder(orderId);
			return AppUtil.returnMapJson("1", msg+"成功!", null);
    		
		} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
}
